package com.lin.service.translate;

import com.lin.common.utils.HttpClientUtils;
import com.lin.common.vo.TranslateReqMsg;
import com.lin.service.customer.CustomerService;
import com.lin.socketio.dto.ChatObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.Header;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.util.Map;

@Component
public class TranslateServiceImpl implements TranslateService {
    private static Logger log = LoggerFactory.getLogger(TranslateServiceImpl.class);
    @Value("${cs.translate.url}")
    private String translateUrl;
    @Value("${cs.translate.test-flag}")
    private Boolean testFlag;

    @Autowired
    private CustomerService customerService;

    @Override
    public ChatObject translate(ChatObject co) throws UnsupportedEncodingException {
        Map<Long, String> map = customerService.listnations();
        //如果是同样的语言，则不需要做翻译【这里可能会导致BUG，即本身是ZH语言，但是我现在设置成EN，另外一个台湾的设置成EN，那么这样是会出现这样就没有翻译；
        //if(StringUtils.equals(map.get(data.getSender()),map.get(data.getRecipient()))){}
        //TODO 如何设置发送者的信息和接收者的信息
        TranslateReqMsg tr = new TranslateReqMsg();
        if (StringUtils.isNotBlank(map.get(co.getSender()))) {
            tr.setTargetLanguage(map.get(co.getSender()));
            tr.setTestFlag(testFlag);
            tr.setText(co.getContent());
            String result = HttpClientUtils.httpPost(translateUrl, tr, null);
            log.info("source conent is {} \n translate contents is {}", co.getContent(), result);
            co.setSenderTranslateContent(result);
        }
        if (StringUtils.isNotBlank(map.get(co.getRecipient()))) {
            tr.setTargetLanguage(map.get(co.getRecipient()));
            tr.setTestFlag(testFlag);
            tr.setText(co.getContent());
            String result = HttpClientUtils.httpPost(translateUrl, tr, null);
            log.info("source conent is {} \n RecipientTranslate contents is {}", co.getContent(), result);
            co.setRecipientTranslateContent(result);
        }
        return co;
    }

}
